define(['template', 'jquery'], (template) => {
    class Header {
      constructor () {
        //一定会用 直接调用
        this.loadHTML().then(() => {
          // 只要涉及header的一些DOM操作就都在这里写
            this.getList()  
            this.calcCartCount()
            this.isLogin()
            this.scroll()
        })
      }
      //将html的内容加载出来
      loadHTML () {
        return new Promise((resolve, reject) => {
          $('header').load('/html/modules/header.html', () => {
            resolve()
          })
        })
      }
      getList(){
        $('.unlogin').show();
        $('.logined').hide();
        //调用template方法 需要先依赖它
        $.get('http://www.xiongmaoyouxuan.com/api/tab/1',{start:0},resp=>{
          // console.log(resp)
            var list = resp.data.gridsV2;
            $('.menu-ul').html(template('listTemplate',{list}));
        })
      }

      calcCartCount (){
        let count = 0
        let cart = localStorage.getItem('cart');
        if(cart){
          cart = JSON.parse(cart);
          count = cart.reduce((num,shop)=>{
            num += shop.count;
            return num;
          },0)
        }
        $("#header-cart").html(count);
        $(".navico-cartnum").html(count);
      }


      //判断cookie是否存在 也就是是否登录
      isLogin(){
        var cookie = this.getCookie('name');
        if(cookie){
          $('.unlogin').hide();
          $('.logined').show();
          $('.login-name').html(cookie);
          $('.tuichu').on('click',()=>{
            if(confirm("确定退出吗")){
              this.setCookie("name","",{expires:-1,path:"/"});
              $('.unlogin').show();
              $('.logined').hide();
            }
          })
        }else{
          $('.unlogin').show();
          $('.logined').hide();
        }
      }

      //获取cookie
      getCookie (key) {
        var str = document.cookie
        var arr = str.split('; ')
        var obj = {}
        arr.forEach(function (cookie) {
          var arr1 = cookie.split('=')
          // 把arr1[0]作为属性名，arr1[1]作为属性值放入obj里
          // 取值的时候解码
          obj[arr1[0]] = decodeURIComponent(arr1[1])
        })
        return obj[key]
      }

      //设置cookie
      setCookie (key, value, option) {
        // 存的时候编码
        var str = `${key}=${encodeURIComponent(value)}`
        if (option) {
          if (option.path) {
            str += `;path=${option.path}`
          }
          if (option.expires) {
            // 有过期时间
            var date = new Date()
            date.setDate(date.getDate() + option.expires)
            str += `;expires=${date.toUTCString()}`
          }
        }
        document.cookie = str
      }

      //吸顶
      scroll(){
        var titleTop = $('.top-action').offset().top;
        // var link = true;
        $(window).scroll(function(){
          if($(window).scrollTop() >= 580 ){
            
            $('.top-action').css({
              "position":"fixed",
              "animation": "mymove 1s forwards",
              "z-index":999,
            })
            // link = false
            // $('<div id="linshi" style="height:186px">').appendTo($("header"));
          }else{
            $('.top-action').css({
              "position":"relative",
              "left":0,
              "top":0,
              "animation": "none",
            })
            // $("#linshi").remove();
          }
          return false
        })
        // console.log($(window).scrollTop())
      }
    }
    return new Header()
  })
  